/*
 * FrameContainer.java
 *
 * Created on 19 nov. 2011, 18:07:55
 */
package com.sma.renamefilewindows.ihm;

import com.sma.renamefilewindows.ihm.panel.AddTextePnl;
import com.sma.renamefilewindows.ihm.panel.RendreSequentielPnl;
import com.sma.renamefilewindows.ihm.panel.UpdaterListeFichier;
import com.sma.renamefilewindows.utils.classe.Couple;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 *
 * @author SMA
 * @version 1.0
 */
public class FrameContainer extends javax.swing.JFrame {

    private UpdaterListeFichier updateListeFichier;
    private HashMap<File, String> lstFichier = null;
    private CardLayout cards = null;
    private boolean initialisation = true;
    public static final String NAMES_FILES_EXEMPLE = "Nom élément.xxx";
    public static final String NAME_FILE_EXEMPLE = "xxxxxxxx.xxx";

    /** Creates new form FrameContainer */
    public FrameContainer() {
        __initComponents();
    }

    private void __initComponents() {
        initComponents();
        __initMenu();

        lstFichier = new HashMap<File, String>();
        cards = new CardLayout();
        this.panelContainer.setLayout(cards);
        AddTextePnl pnlTexte = new AddTextePnl(this);
        this.panelContainer.add(pnlTexte, Actions.ActionFile.ADD_TEXTE.name());
        this.panelContainer.add(new RendreSequentielPnl(this), Actions.ActionFile.SEQUENCE.name());
        cards.show(this.panelContainer, Actions.ActionFile.ADD_TEXTE.name());
        this.updateListeFichier = pnlTexte;

        Actions.initJComboBoxAction(this.typeModification);
        this.labelExemple.setText(NAMES_FILES_EXEMPLE);

        this.initialisation = false;
        this.pack();
        this.setVisible(true);

        this.methodOuvrirFichier();
    }

    public boolean isInitialisation() {
        return initialisation;
    }

    private void __initMenu() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menuFichier = new JMenu("Fichier");
        JMenuItem itemOuvrir = new JCheckBoxMenuItem("Ouvrir");
        itemOuvrir.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                FrameContainer.this.methodOuvrirFichier();
            }
        });

        JMenuItem itemQuitter = new JCheckBoxMenuItem("Quitter");
        menuFichier.add(itemOuvrir);
        menuFichier.add(itemQuitter);
        menuBar.add(menuFichier);

        this.setJMenuBar(menuBar);
        //getJMenuBar().setPreferredSize(new Dimension(400, 100));

    }

    public void setLabelExemple(String texte) {
        this.labelExemple.setText(texte);
    }

    public void setLstFichier(HashMap<File, String> lstFichier) {
        this.lstFichier = lstFichier;
    }

    public HashMap<File, String> getLstFichier() {
        return lstFichier;
    }

    private void addFileToList(File fichier){
        if(fichier.isDirectory()){
            for(File file : fichier.listFiles()){
                addFileToList(file);
            }
        }else{
            this.lstFichier.put(fichier,null);
        }
        
        
    }
    private void methodOuvrirFichier() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setMultiSelectionEnabled(true);
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        int retour = fileChooser.showOpenDialog(FrameContainer.this);
        lstFichier.clear();
        if (retour == JFileChooser.APPROVE_OPTION) {
            for (File fichier : fileChooser.getSelectedFiles()) {
                addFileToList(fichier);
            }

        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        typeModification = new javax.swing.JComboBox();
        panelContainer = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        labelExemple = new javax.swing.JLabel();
        btnValider = new javax.swing.JButton();
        btnAnnuler = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        typeModification.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        typeModification.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                typeModificationItemStateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout panelContainerLayout = new org.jdesktop.layout.GroupLayout(panelContainer);
        panelContainer.setLayout(panelContainerLayout);
        panelContainerLayout.setHorizontalGroup(
            panelContainerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 450, Short.MAX_VALUE)
        );
        panelContainerLayout.setVerticalGroup(
            panelContainerLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 109, Short.MAX_VALUE)
        );

        jLabel1.setText("Exemple :");

        labelExemple.setText("xxxxxxxxxxxxx.xxx");

        btnValider.setText("Valider");
        btnValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnValiderActionPerformed(evt);
            }
        });

        btnAnnuler.setText("Quitter");
        btnAnnuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnnulerActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(24, 24, 24)
                        .add(panelContainer, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jLabel1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(labelExemple))
                            .add(typeModification, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 188, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 86, Short.MAX_VALUE)
                        .add(btnAnnuler)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnValider)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(typeModification, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(panelContainer, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(labelExemple)
                    .add(btnValider)
                    .add(btnAnnuler))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnValiderActionPerformed
    if (lstFichier.isEmpty()) {
        JOptionPane.showMessageDialog(this, "Erreur", "Aucun fichier selectionné", JOptionPane.ERROR_MESSAGE);
        return;
    }
    this.updateListeFichier.updateListeFichier();
    for (File fichier : lstFichier.keySet()) {
        String newFileName = lstFichier.get(fichier);
        File newFile = new File(newFileName);
        fichier.renameTo(newFile);
        System.out.println("*************");
        System.out.println("\t\tAncien fichier : " + fichier.getAbsolutePath());
        System.out.println("\t\tNouveau fichier : " + newFile.getAbsolutePath());

    }
    JOptionPane.showMessageDialog(this, "Terminée !", "Résultat", JOptionPane.INFORMATION_MESSAGE);
    lstFichier.clear();
}//GEN-LAST:event_btnValiderActionPerformed

private void btnAnnulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnnulerActionPerformed
    this.dispose();
}//GEN-LAST:event_btnAnnulerActionPerformed

private void typeModificationItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_typeModificationItemStateChanged
    Couple<Actions.ActionFile> action = (Couple<Actions.ActionFile>) this.typeModification.getSelectedItem();
    if (action != null && cards != null) {
        switch (action.getEnumObject()) {
            case ADD_TEXTE:
                cards.show(this.panelContainer, Actions.ActionFile.ADD_TEXTE.name());
                this.updateListeFichier = (UpdaterListeFichier) this.panelContainer.getComponent(0);
                break;
            case SEQUENCE:
                cards.show(this.panelContainer, Actions.ActionFile.SEQUENCE.name());
                this.updateListeFichier = (UpdaterListeFichier) this.panelContainer.getComponent(1);
                break;
        }
    }
    this.pack();
    this.setVisible(true);
}//GEN-LAST:event_typeModificationItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnnuler;
    private javax.swing.JButton btnValider;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labelExemple;
    private javax.swing.JPanel panelContainer;
    private javax.swing.JComboBox typeModification;
    // End of variables declaration//GEN-END:variables
    private Actions.ActionFile action = Actions.ActionFile.ADD_TEXTE;

    private static class Actions {

        public static interface LibelleAction {

            public static final String ADD_TEXTE = "Ajouter au texte";
            public static final String SEQUENTIEL = "Rendre séquentiel";
        }

        public static enum ActionFile {

            ADD_TEXTE, SEQUENCE
        }

        public static void initJComboBoxAction(JComboBox comboBoxAction) {
            comboBoxAction.removeAllItems();
            comboBoxAction.addItem(new Couple<ActionFile>(ActionFile.ADD_TEXTE, LibelleAction.ADD_TEXTE));
            comboBoxAction.addItem(new Couple<ActionFile>(ActionFile.SEQUENCE, LibelleAction.SEQUENTIEL));
        }
    }
}
